package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.CheckItemService;
import com.itheima.service.OrderSettingService;
import com.sun.corba.se.spi.ior.ObjectKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author FCJ
 * @date 2020-01-05 11:59
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;



    @Override
    public void addOrderSetting(List<OrderSetting> orderSettings) {
        if (orderSettings != null) {
            for (OrderSetting orderSetting : orderSettings) {
                if (orderSettingDao.findCountByDate(orderSetting.getOrderDate()) > 0) {
                    orderSettingDao.updateNumber(orderSetting);
                } else {
                    orderSettingDao.addOrderSetting(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map> findOrderListByMonth(String date) {
        String begin=date+"-1";
        String end=date+"-31";
        Map map=new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        List<OrderSetting> list=orderSettingDao.findOrderListByMonth(map);
        ArrayList<Map> resultList = new ArrayList<>();
        if (list!=null&&list.size()>0){
            for (OrderSetting orderSetting:list){
                Map result = new HashMap<>();
                result.put("date",orderSetting.getOrderDate().getDate());
                result.put("number",orderSetting.getNumber());
                result.put("reservations",orderSetting.getReservations());
                resultList.add(result);
            }
        }
        return resultList;
    }

    @Override
    public void updateOneOrderSet(OrderSetting orderSetting) {
//        orderSettingDao.updateNumber(orderSetting);
        if (orderSettingDao.findCountByDate(orderSetting.getOrderDate()) > 0) {
            orderSettingDao.updateNumber(orderSetting);
        } else {
            orderSettingDao.addOrderSetting(orderSetting);
        }
    }


    }

